﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.UI.Controls
{
    /// <summary>
    /// Interaction logic for TimeControl.xaml
    /// </summary>
    public partial class TimeControl : UserControl
    {

        public TimeSpan Value
        {
            get { return (TimeSpan)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(TimeSpan), typeof(TimeControl), new UIPropertyMetadata(TimeSpan.MinValue, new PropertyChangedCallback(Value_Changed)));

        public Int32 Hour
        {
            get { return (Int32)GetValue(HourProperty); }
            set { SetValue(HourProperty, value); }
        }

        public static readonly DependencyProperty HourProperty =
            DependencyProperty.Register("Hour", typeof(Int32), typeof(TimeControl), new UIPropertyMetadata(0, new PropertyChangedCallback(Hour_Changed)));

        public Int32 Minute
        {
            get { return (Int32)GetValue(MinuteProperty); }
            set { SetValue(MinuteProperty, value); }
        }

        public static readonly DependencyProperty MinuteProperty =
            DependencyProperty.Register("Minute", typeof(Int32), typeof(TimeControl), new UIPropertyMetadata(0, new PropertyChangedCallback(Minute_Changed)));

        private static void Hour_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TimeControl timeControl = (TimeControl)o;
            timeControl.ConvertTimeToTimeSpan(e.NewValue as Int32?, null);
        }

        private static void Minute_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TimeControl timeControl = (TimeControl)o;
            timeControl.ConvertTimeToTimeSpan(null, e.NewValue as Int32?);
        }

        private static void Value_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TimeControl timeControl = (TimeControl)o;
            timeControl.ConvertTimeSpanToTime((TimeSpan)e.NewValue);
        }

        public TimeControl()
        {
            InitializeComponent();
        }

        private void ConvertTimeToTimeSpan(Int32? hour, Int32? minute)
        {
            if (hour.HasValue && hour.Value > 23)
            {
                hour = 23;
                this.Hour = 23;
            }
            if (minute.HasValue && minute.Value > 59)
            {
                minute = 59;
                this.Minute = 59;
            }



            this.Value = new TimeSpan(
                hour.HasValue ? hour.Value : this.Hour, 
                minute.HasValue ? minute.Value : this.Minute, 
                0); 
        }

        private void ConvertTimeSpanToTime(TimeSpan dateTime)
        {
            this.Hour = dateTime.Hours;
            this.Minute = dateTime.Minutes;
        }

    }
}
